﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CE.Database;
using System.Data;
using CE.Global;

public partial class carinsurance_setting_Default : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 360; i > 4; i -= 3)
            {
                ddlQuoteStart.Items.Add(new ListItem(i.ToString() + " Days ago", i.ToString()));
            }

            for (int i = 0; i < 5; i++)
            {
                if (i == 0)
                {
                    ddlQuoteEnd.Items.Add(new ListItem("Today", i.ToString()));
                }
                else if (i == 1)
                {
                    ddlQuoteEnd.Items.Add(new ListItem("Yesterday", i.ToString()));
                }
                else
                {
                    ddlQuoteEnd.Items.Add(new ListItem(i.ToString() + " Days ago", i.ToString()));
                }
            }

            for (int i = 60; i > 7; i--)
            {
                ddlCommEnd.Items.Add(new ListItem(i.ToString() + " ", i.ToString()));
            }

            for (int i = -30; i < 30; i++)
            {
                if (i == 0)
                {
                    ddlCommStart.Items.Add(new ListItem("Today", i.ToString()));
                }
                else if (i == 1)
                {
                    ddlCommStart.Items.Add(new ListItem("Tomorrow", i.ToString()));
                }
                else
                {
                    ddlCommStart.Items.Add(new ListItem(i.ToString() + " Days ", i.ToString()));
                }
            }

            for (int h = 0; h < 24; h++)
            {
                ddlLeadOpenStartTime1Hour.Items.Add(new ListItem(h.ToString("00"), h.ToString("00")));
                ddlLeadOpenStartTime2Hour.Items.Add(new ListItem(h.ToString("00"), h.ToString("00")));
                ddlLeadOpenStartTime3Hour.Items.Add(new ListItem(h.ToString("00"), h.ToString("00")));

                ddlLeadOpenEndTime1Hour.Items.Add(new ListItem(h.ToString("00"), h.ToString("00")));
                ddlLeadOpenEndTime2Hour.Items.Add(new ListItem(h.ToString("00"), h.ToString("00")));
                ddlLeadOpenEndTime3Hour.Items.Add(new ListItem(h.ToString("00"), h.ToString("00")));
            }

            for (int m = 0; m < 60; m+=5)
            {
                ddlLeadOpenStartTime1Min.Items.Add(new ListItem(m.ToString("00"), m.ToString("00")));
                ddlLeadOpenStartTime2Min.Items.Add(new ListItem(m.ToString("00"), m.ToString("00")));
                ddlLeadOpenStartTime3Min.Items.Add(new ListItem(m.ToString("00"), m.ToString("00")));

                ddlLeadOpenEndTime1Min.Items.Add(new ListItem(m.ToString("00"), m.ToString("00")));
                ddlLeadOpenEndTime2Min.Items.Add(new ListItem(m.ToString("00"), m.ToString("00")));
                ddlLeadOpenEndTime3Min.Items.Add(new ListItem(m.ToString("00"), m.ToString("00")));
            }

            btnShowCurrent_Click(sender, e);
        }
    }

    protected void btnShowCurrent_Click(object sender, EventArgs e)
    {
        MyDatabase _db = new MyDatabase();
        SortedList _spara = new SortedList();
        _spara.Add("@SettingID", lblSettingID.Text);

        DataTable _dtSetting = _db.GetDataTableBySP("sprocCXCRMAutoLeadSettingSelectSingleItem", _spara);
        if (_dtSetting != null && _dtSetting.Rows.Count > 0)
        {
            txtMinPremium.Text = _dtSetting.Rows[0]["MinPremium"].ToString();
            ddlQuoteStart.Text = _dtSetting.Rows[0]["QuoteStart"].ToString();
            ddlQuoteEnd.Text = _dtSetting.Rows[0]["QuoteEnd"].ToString();
            ddlCommStart.Text = _dtSetting.Rows[0]["CommDateStart"].ToString();
            ddlCommEnd.Text = _dtSetting.Rows[0]["CommDateEnd"].ToString();
            ddlOrderByOption.Text = _dtSetting.Rows[0]["OrderByOption"].ToString();

            txtLastQuoteMins.Text = _dtSetting.Rows[0]["AfterLastQuoteMins"].ToString();

            TimeSpan _leadOpenStartTime1 = (TimeSpan)_dtSetting.Rows[0]["LeadOpenStartTime1"];
            TimeSpan _leadOpenEndTime1 = (TimeSpan)_dtSetting.Rows[0]["LeadOpenEndTime1"];
            TimeSpan _leadOpenStartTime2 = (TimeSpan)_dtSetting.Rows[0]["LeadOpenStartTime2"];
            TimeSpan _leadOpenEndTime2 = (TimeSpan)_dtSetting.Rows[0]["LeadOpenEndTime2"];
            TimeSpan _leadOpenStartTime3 = (TimeSpan)_dtSetting.Rows[0]["LeadOpenStartTime3"];
            TimeSpan _leadOpenEndTime3 = (TimeSpan)_dtSetting.Rows[0]["LeadOpenEndTime3"];

            ddlLeadOpenStartTime1Hour.Text = _leadOpenStartTime1.Hours.ToString("00");
            ddlLeadOpenStartTime1Min.Text = _leadOpenStartTime1.Minutes.ToString("00");
            ddlLeadOpenEndTime1Hour.Text = _leadOpenEndTime1.Hours.ToString("00");
            ddlLeadOpenEndTime1Min.Text = _leadOpenEndTime1.Minutes.ToString("00");

            ddlLeadOpenStartTime2Hour.Text = _leadOpenStartTime2.Hours.ToString("00");
            ddlLeadOpenStartTime2Min.Text = _leadOpenStartTime2.Minutes.ToString("00");
            ddlLeadOpenEndTime2Hour.Text = _leadOpenEndTime2.Hours.ToString("00");
            ddlLeadOpenEndTime2Min.Text = _leadOpenEndTime2.Minutes.ToString("00");

            ddlLeadOpenStartTime3Hour.Text = _leadOpenStartTime3.Hours.ToString("00");
            ddlLeadOpenStartTime3Min.Text = _leadOpenStartTime3.Minutes.ToString("00");
            ddlLeadOpenEndTime3Hour.Text = _leadOpenEndTime3.Hours.ToString("00");
            ddlLeadOpenEndTime3Min.Text = _leadOpenEndTime3.Minutes.ToString("00");
        }

        btnPreview_Click(sender, e);
    }

    protected void btnPreview_Click(object sender, EventArgs e)
    {
        MyDatabase _db = new MyDatabase();
        DataSet _dsResult = new DataSet();

        SortedList _spara = new SortedList();
        _spara.Add("@OrderByOption", ddlOrderByOption.Text);
        _spara.Add("@MinPremium", txtMinPremium.Text);
        _spara.Add("@QuoteDateStart", ddlQuoteStart.Text);
        _spara.Add("@QuoteDateEnd", ddlQuoteEnd.Text);
        _spara.Add("@CommenceDateStart", ddlCommStart.Text);
        _spara.Add("@CommenceDateEnd", ddlCommEnd.Text);

        _dsResult = _db.GetDataSet("sprocCXCRMGetCarDetailSelectListBySetting", _spara);
        if (_dsResult != null && _dsResult.Tables.Count > 0)
        {
            gvSetting.DataSource = _dsResult.Tables[0];
            gvSetting.DataBind();
            if (_dsResult.Tables.Count > 1)
            {
                gvResult.DataSource = _dsResult.Tables[1];
                gvResult.DataBind();
                if (_dsResult.Tables.Count > 2)
                {
                    gvCustomerCount.DataSource = _dsResult.Tables[2];
                    gvCustomerCount.DataBind();
                }
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MyDatabase _db = new MyDatabase();

        SortedList _spara = new SortedList();
        _spara.Add("@SettingID", lblSettingID.Text);
        _spara.Add("@MinPremium", txtMinPremium.Text);
        _spara.Add("@QuoteStart", ddlQuoteStart.Text);
        _spara.Add("@QuoteEnd", ddlQuoteEnd.Text);
        _spara.Add("@CommDateStart", ddlCommStart.Text);
        _spara.Add("@CommDateEnd", ddlCommEnd.Text);
        _spara.Add("@OrderByOption", ddlOrderByOption.Text);
        _spara.Add("@RefUserID", Session[General.UserId].ToString());
        _spara.Add("@AfterLastQuoteMins", txtLastQuoteMins.Text);
        _spara.Add("@LeadOpenStartTime1", ddlLeadOpenStartTime1Hour.Text + " : " + ddlLeadOpenStartTime1Min.Text);
        _spara.Add("@LeadOpenEndTime1", ddlLeadOpenEndTime1Hour.Text + " : " + ddlLeadOpenEndTime1Min.Text);
        _spara.Add("@LeadOpenStartTime2", ddlLeadOpenStartTime2Hour.Text + " : " + ddlLeadOpenStartTime2Min.Text);
        _spara.Add("@LeadOpenEndTime2", ddlLeadOpenEndTime2Hour.Text + " : " + ddlLeadOpenEndTime2Min.Text);
        _spara.Add("@LeadOpenStartTime3", ddlLeadOpenStartTime3Hour.Text + " : " + ddlLeadOpenStartTime3Min.Text);
        _spara.Add("@LeadOpenEndTime3", ddlLeadOpenEndTime3Hour.Text + " : " + ddlLeadOpenEndTime3Min.Text);

        int _settingID = _db.ExecuteNonSqlSP("sprocCXCRMAutoLeadSettingUpdate", _spara);

        if (_settingID > 0)
        {
            lblMessage.Text = "Updated successfully.";
            lblMessage.ForeColor = System.Drawing.Color.Green;
            btnPreview_Click(sender, e);
        }
        else
        {
            lblMessage.Text = "Failed to update.";
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }
    }
}
